package com.pn.warehouse.controller;

import com.pn.warehouse.entity.InStore;
import com.pn.warehouse.entity.OutStore;
import com.pn.warehouse.entity.Result;
import com.pn.warehouse.entity.Store;
import com.pn.warehouse.page.Page;
import com.pn.warehouse.service.OutStoreService;
import com.pn.warehouse.service.StoreService;
import com.pn.warehouse.utils.TokenUtils;
import com.pn.warehouse.utils.WarehouseConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/outstore")
@RestController
public class OutStoreController {

    @Autowired
    private OutStoreService outStoreService;

    @Autowired
    private TokenUtils tokenUtils;

    @Autowired
    private StoreService storeService;

    /**
     * 添加出库单
     */
    @RequestMapping("/outstore-add")
    public Result addOutStore(@RequestBody OutStore outStore, @RequestHeader(WarehouseConstants.HEADER_TOKEN_NAME) String token){
        //1、拿到当前用户的id
        int userId = tokenUtils.getCurrentUser(token).getUserId();

        //2、设置创建人
        outStore.setCreateBy(userId);

        //3、执行业务并返回
        Result result = outStoreService.insertOutStore(outStore);
        return result;
    }

    /**
     * 查询所有的仓库
     */
    @RequestMapping("/store-list")
    public Result storeList(){
        //1、执行业务并返回
        List<Store> allStore = storeService.findAllStore();
        return Result.ok(allStore);
    }

    /**
     * 分页查询出库单
     */
    @RequestMapping("/outstore-page-list")
    public Result outStoreListPage(Page page, OutStore outStore){
        //1、执行业务并返回
        page = outStoreService.queryOutStorePage(page, outStore);
        return Result.ok(page);
    }

    /**
     * 确认出库
     */
    @RequestMapping("/outstore-confirm")
    public Result confirmOutStore(@RequestBody OutStore outStore){
        //1、执行业务并返回
        Result result = outStoreService.outStoreConfirm(outStore);
        return result;
    }

}
